package com.lc.YYGH.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.YYGH.cmn.listener.DictListener;
import com.lc.YYGH.cmn.mapper.DictMapper;
import com.lc.YYGH.cmn.service.DictService;
import com.lc.YYGH.model.cmn.Dict;
import com.lc.YYGH.vo.cmn.DictEeVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.KeyGenerator;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    @Cacheable(value="dict",keyGenerator = "keyGenerator")    //redis用的
    public List<Dict> findChildData(Long id)
    {
        QueryWrapper<Dict> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> dictList=baseMapper.selectList(wrapper);
        for(Dict dict:dictList)
        {

            boolean isChild=this.isChildren(dict.getId());
            dict.setHasChildren(isChild);
        }
        return dictList;
    }
    private boolean isChildren(Long id)
    {
        QueryWrapper<Dict> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count=baseMapper.selectCount(wrapper);
        return count>0;
    }


    @Override
    public void exportData(HttpServletResponse response) {
        try{
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName=URLEncoder.encode("数据字典","UTF-8");
            System.out.println("运行到1");
            response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
            List<Dict> dictList=dictMapper.selectList(null);
            List<DictEeVo> dictVoList=new ArrayList<>(dictList.size());
            for(Dict dict:dictList)
            {
                DictEeVo dictVo=new DictEeVo();

                //把dict对象中的属性数据拷贝到dictVo对象的同名属性
                BeanUtils.copyProperties(dict,dictVo,DictEeVo.class);
                dictVoList.add(dictVo);
            }
            System.out.println("运行到2");
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("数据字典").doWrite(dictVoList);

        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
    @Override
    @CacheEvict(value="dict",allEntries = true)
    public void importDictData(MultipartFile file) {
        try
        {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictMapper)).sheet().doRead();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
    @Cacheable(value="dict",keyGenerator="keyGenerator")
    @Override
    public String getNameByParentDictCodeAndValue(String parentDictCode, String value) {
        if(StringUtils.isEmpty(parentDictCode))
        {
           QueryWrapper<Dict> wrapper=new QueryWrapper<>();
           wrapper.select("name").eq("value",value);
           Dict dict=dictMapper.selectOne(wrapper);
           return dict.getName();
        }
        else {
            QueryWrapper<Dict> wrapper=new QueryWrapper<>();
            wrapper.select("id").eq("dict_code",parentDictCode);
            Dict dict=dictMapper.selectOne(wrapper);
            wrapper=new QueryWrapper<>();
            wrapper.eq("parent_id",dict.getId()).eq("value",value);
            Dict dict1=dictMapper.selectOne(wrapper);
            return dict1.getName();
        }




    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        QueryWrapper<Dict> wrapper=new QueryWrapper<>();
        wrapper.select("id").eq("dict_code",dictCode);
        Dict dict=dictMapper.selectOne(wrapper);
//        wrapper=new QueryWrapper<>();
//        wrapper.eq("parent_id",dict.getId());
//        List<Dict> dicts=dictMapper.selectList(wrapper);
        //return dicts;
        List<Dict> dicts=this.findChildData(dict.getId());
        return dicts;
    }
}
